package com.adonis.ejoy;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.adonis.ejoy.bean.User;
import com.adonis.ejoy.fragment.HomeFragment;
import com.adonis.ejoy.fragment.MeFragment;
import com.adonis.ejoy.fragment.PostFragment;
import com.adonis.ejoy.fragment.SearchFragment;

import org.litepal.tablemanager.Connector;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.user_info)
    TextView user_info;
    @BindView(R.id.layout_home)
    LinearLayout layout_home;
    @BindView(R.id.text_home)
    TextView text_home;
    @BindView(R.id.image_home)
    ImageView image_home;
    @BindView(R.id.text_add)
    TextView text_add;
    @BindView(R.id.image_add)
    ImageView image_add;
    @BindView(R.id.text_search)
    TextView text_search;
    @BindView(R.id.image_search)
    ImageView image_search;
    @BindView(R.id.text_me)
    TextView text_me;
    @BindView(R.id.image_me)
    ImageView image_me;

    private int uid;
    private Fragment homefragment;
    private Fragment postfragment;
    private Fragment searchfragment;
    private Fragment mefragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        uid = getIntent().getIntExtra("uid",0);
        user_info.setMaxWidth(uid);
        replaceFragment(homefragment = new HomeFragment());
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.top_layout,fragment);
        transaction.commit();
    }

    @OnClick({R.id.layout_home,R.id.layout_add,R.id.layout_search,R.id.layout_me})
    public void onClick(View v) {
         final int color_select = getResources().getColor(R.color.select);
         switch (v.getId()) {
            case R.id.layout_home:
                if(text_home.getCurrentTextColor() != color_select){
                    replaceFragment(homefragment);
                    text_home.setTextColor(color_select);
                    image_home.setImageResource(R.drawable.home_selected);
                    text_add.setTextColor(Color.BLACK);
                    image_add.setImageResource(R.drawable.add_noselected);
                    text_search.setTextColor(Color.BLACK);
                    image_search.setImageResource(R.drawable.search_noselected);
                    text_me.setTextColor(Color.BLACK);
                    image_me.setImageResource(R.drawable.me_noselected);
                }
                break;
            case R.id.layout_add:
                if(text_add.getCurrentTextColor() != color_select){
                    replaceFragment(postfragment = postfragment != null ? postfragment:new PostFragment());
                    text_home.setTextColor(Color.BLACK);
                    image_home.setImageResource(R.drawable.home_noselected);
                    text_add.setTextColor(color_select);
                    image_add.setImageResource(R.drawable.add_selected);
                    text_search.setTextColor(Color.BLACK);
                    image_search.setImageResource(R.drawable.search_noselected);
                    text_me.setTextColor(Color.BLACK);
                    image_me.setImageResource(R.drawable.me_noselected);
                }
                break;
            case R.id.layout_search:
                if(text_search.getCurrentTextColor() != color_select) {
                    replaceFragment(searchfragment = searchfragment != null ? searchfragment:new SearchFragment());
                    text_home.setTextColor(Color.BLACK);
                    image_home.setImageResource(R.drawable.home_noselected);
                    text_add.setTextColor(Color.BLACK);
                    image_add.setImageResource(R.drawable.add_noselected);
                    text_search.setTextColor(color_select);
                    image_search.setImageResource(R.drawable.search_selected);
                    text_me.setTextColor(Color.BLACK);
                    image_me.setImageResource(R.drawable.me_noselected);
                }
                break;
            case R.id.layout_me:
                if(text_me.getCurrentTextColor() != color_select) {
                    replaceFragment(mefragment = mefragment != null ? mefragment:new MeFragment());
                    text_home.setTextColor(Color.BLACK);
                    image_home.setImageResource(R.drawable.home_noselected);
                    text_add.setTextColor(Color.BLACK);
                    image_add.setImageResource(R.drawable.add_noselected);
                    text_search.setTextColor(Color.BLACK);
                    image_search.setImageResource(R.drawable.search_noselected);
                    text_me.setTextColor(color_select);
                    image_me.setImageResource(R.drawable.me_selected);
                }
                break;
        }
    }

}
